package cn.dkk.Spring.framework.context.support;

import cn.dkk.Spring.framework.beans.factory.support.BeanDefinitionReader;
import cn.dkk.Spring.framework.beans.factory.support.BeanDefinitionRegistry;
import cn.dkk.Spring.framework.context.ApplicationContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author dkk
 * @date 2022/8/3
 * @desc ApplicationContext抽象子实现类
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    protected BeanDefinitionReader beanDefinitionReader;

    protected Map<String,Object> singletonObjects = new ConcurrentHashMap<>();

    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        // 加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        finishBeanDefinition();
    }

    private void finishBeanDefinition() throws Exception {
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        String[] beanNames = registry.getBeanDefinitionName();
        for (String beanName : beanNames) {
            Object bean = getBean(beanName);
            singletonObjects.put(beanName,bean);
        }
    }
}
